import 'package:flutter/material.dart';
import 'package:flutter_hplus/components/tables/demo.dart';
import 'package:flutter_hplus/components/tables/index.dart';
import 'package:flutter_hplus/pages/system/roles/system_role_list_page.dart';
import 'package:flutter_hplus/pages/system/system_home_page.dart';
import 'package:flutter_hplus/pages/system/users/system_user_list_page.dart';
import '../pages/home/home_page.dart';
import '../pages/home/home_page6.dart';
import '../pages/home/home_page7.dart';
import '../pages/home/home_page8.dart';
import '../pages/home/home_page9.dart';
import '../pages/dashboard/dashboard_page.dart';
import '../pages/forms/basic_forms_page.dart';
import '../pages/forms/form_basic_page.dart';
import '../pages/tables/basic_table_page.dart';
import '../pages/tables/data_table_page.dart';
import '../storybook/button_page.dart';
import '../storybook/theme_demo_page.dart';
import '../storybook/tabs_demo_page.dart';

/// 菜单项数据模型
class SideMenuItem {
  final String title;
  final IconData? icon;
  final String route;
  final Widget? page;
  final List<SideMenuItem>? children;
  final String? fullPath; // 完整路径，包含父级路径

  const SideMenuItem({
    required this.title, 
    required this.route, 
    this.icon, 
    this.page,
    this.children,
    this.fullPath,
  });

  bool get hasChildren => children != null && children!.isNotEmpty;
  
  // 获取标准路由名称（去掉前导斜杠）
  String get normalizedRoute => route.startsWith('/') ? route.substring(1) : route;
  
  // 获取完整路径（如果提供了fullPath则使用，否则使用route）
  String get completePath => fullPath ?? route;
}


/// 快速创建菜单项 - 向后兼容的版本
/// 构建带有构建器的菜单项 - 可直接用于路由配置
SideMenuItem menu(String title, String route, IconData icon, {Widget? page, List<SideMenuItem>? children}) {
  return SideMenuItem(title: title, route: route, icon: icon, page: page, children: children);
}


/// 获取应用程序的所有菜单项 - 同时包含路由配置
List<SideMenuItem> getSideMenuItems() {
  return [
    menu('系统管理', '/system', Icons.home, page: const SystemHomePage(), children: [
      menu('用户', '/users', Icons.person, page: const SystemUserListPage()), 
      menu('角色', '/roles', Icons.home_outlined, page: const SystemRoleListPage()),
      menu('权限', '/resources', Icons.home_work, page: const HomePage8()),
    ]),
    menu('元数据', '/metadata', Icons.home, page: const HomeContent(), children: [
      menu('字典管理', '/dictionary', Icons.home_max, page: const HomePage7()),
      menu('字段管理', '/field', Icons.home, page: const HomePage6()),
      menu('字段组管理', '/field_group', Icons.home_outlined, page: const HomePage7()),
      menu('字段类型管理', '/field_type', Icons.home_work, page: const HomePage8()),
      menu('字段组类型管理', '/field_group_type', Icons.home_max, page: const HomePage9()),
    ]),
    menu('首页', '/home', Icons.home, page: const HomeContent(), children: [
      menu('首页6 - 简约风格', '/home6', Icons.home, page: const HomePage6()),
      menu('首页7 - 简约风格', '/home7', Icons.home_outlined, page: const HomePage7()),
      menu('首页8 - 深色主题风格', '/home8', Icons.home_work, page: const HomePage8()),
      menu('首页9 - 极简风格', '/home9', Icons.home_max, page: const HomePage9()),
    ]),
    menu('仪表盘', '/dashboard', Icons.dashboard, children: [
      menu('控制台', '/dashboard', Icons.dashboard, page: const DashboardPage()),
      menu('数据统计', '/analytics', Icons.bar_chart, page: const DashboardPage(title: '数据统计')),
    ]),
    menu('组件', '/components', Icons.widgets, children: [
      menu('主题', '/theme', Icons.palette, page: const ThemeDemoPage()),
      menu('按钮', '/buttons', Icons.smart_button, page: const ButtonPage()),
      menu('基本表单', '/form_basic', Icons.article, page: const FormBasicPage()),
      menu('基础表单', '/forms', Icons.article, page: const BasicFormsPage()),
      menu('表格', '/tables', Icons.table_chart, page: const DataTableDemo()),
      menu('各种表格', '/data_table', Icons.table_chart, page: const DataTablePage()),
      menu('卡片', '/cards', Icons.credit_card, page: const DashboardPage(title: '卡片组件')),
      menu('标签页', '/tabs', Icons.tab, page: const TabsDemoPage()),
    ]),
    menu('页面', '/pages', Icons.pages, children: [
      menu('登录', '/login', Icons.login, page: const DashboardPage(title: '登录页面')),
      menu('注册', '/register', Icons.app_registration, page: const DashboardPage(title: '注册页面')),
      menu('错误页面', '/error', Icons.error, children: [
        menu('403', '/403', Icons.error, page: const DashboardPage(title: '403页面')),
        menu('404', '/404', Icons.error, page: const DashboardPage(title: '404页面')),
        menu('500', '/500', Icons.error, page: const DashboardPage(title: '500页面')),
      ]),
    ]),

  ];
}

/// 展平菜单项树，获取所有路由相关的菜单项
List<SideMenuItem> getFlattenedMenuItems() {
  final allItems = <SideMenuItem>[];
  void flattenMenu(List<SideMenuItem> items) {
    for (final item in items) {
      // 添加所有菜单项
      allItems.add(item);
      
      if (item.hasChildren) {
        flattenMenu(item.children!);
      }
    }
  }
  
  flattenMenu(getSideMenuItems());
  return allItems;
}

